import net.spreadshirt.shop.lib.BasketItem;

/**
 * The basket class stores all basket item objects
 * and also all basket specific values.
 */
class net.spreadshirt.shop.lib.Basket {

	// A list of all basket items.
	private var _items:Array;

	// Various basket values.
	private var _gross_basket:Number;
	private var _gross_shipping:Number;
	private var _gross_handling:Number;
	private var _gross_discount:Number;
	private var _gross_coupon:Number;
	private var _gross_total:Number;
	private var _vat_basket:Number;
	private var _vat_total:Number;
	private var _currency:String;

	/**
	 * Basket Constructor
	 */
	public function Basket() {
		clear();
	}

	/**
	 * Clears all values.
	 */
	public function clear():Void {
		_items = new Array();
		_gross_basket = null;
		_gross_shipping = null;
		_gross_handling = null;
		_gross_discount = null;
		_gross_coupon = null;
		_gross_total = null;
		_vat_basket = null;
		_vat_total = null;
		_currency = null;
	}

	/**
	 * Stores the basket values and all basket items.
	 * 
	 * @param	basket	The basket object.
	 */
	public function setBasket(basket:Object):Void {
		// Clear basket.
		clear();
		// Store basket values.
		_gross_basket = basket.gross_basket;
		_gross_shipping = basket.gross_shipping;
		_gross_handling = basket.gross_handling;
		_gross_discount = basket.gross_discount;
		_gross_coupon = basket.gross_coupon;
		_gross_total = basket.gross_total;
		_vat_basket = basket.vat_basket;
		_vat_total = basket.vat_total;
		_currency = basket.currency;
		// Create new list of basket items.
		for(var i:Number = 0; i < basket.items.length; i++) {
			var item:BasketItem = new BasketItem(basket.items[i]);
			_items.push(item);
		}
	}

	/**
	 * Returns the basket data.
	 * 
	 * @return	The basket object.
	 */
	public function getBasket():Object {
		var basket:Object = new Object();
		basket.gross_baske = _gross_basket; //############################## TYPO ##################### basket.gross_basket
		basket.gross_shipping = _gross_shipping;
		basket.gross_handling = _gross_handling;
		basket.gross_discount = _gross_discount;
		basket.gross_coupon = _gross_coupon;
		basket.gross_total = _gross_total;
		basket.vat_basket = _vat_basket;
		basket.vat_total = _vat_total;
		basket.currency = _currency;
		basket.items = getAllItems();
		return basket;
	}

	/**
	 * Returns the number of stored basket items.
	 * 
	 * @return	The number of basket items.
	 */
	public function getCount():Number {
		return _items.length;
	}

	/**
	 * Returns the complete list of all basket items.
	 * 
	 * @return	The list of basket items.
	 */
	public function getAllItems():Array {
		return _items;
	}

}
